//引入less样式
require('../less/home.less');

//引入字体样式
require('../fonts/iconfont.css');

//引入轮播图
require('../lib/swiper/swiper.css')




//文档加载事件

document.ready(function () {

    //判断用户是否登录
    until.isLogin()


    //获取元素
    const rank = document.querySelector('#rank')
    const punch = document.querySelector('#punch')
    const today = document.querySelector('#today')
    const medal = document.querySelector('#medal')




    //动态导航
    until.showTabs('home')


    //轮播图
    function SwiperHome() {
        var mySwiper = new Swiper('.swiper-container', {
            loop: true, // 循环模式选项

            //自动切换开启，默认停留三秒
            autoplay: {
                delay: 1500,
                stopOnLastSlide: false,
                disableOnInteraction: true,
            },

            // 如果需要分页器
            pagination: {
                el: '.swiper-pagination',
            },

            // 如果需要前进后退按钮
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
        })
    }
    SwiperHome()


    //获取首页数据
    function getHomeDate() {
        //获取本地存储中用户id
        const userId = localStorage.getItem('userId')

        //发送请求
        $http.get(`/headPageInfo?userId=${userId}`, function (res) {
            
            
            if (res.status === 0) {
                //渲染数据
                rank.textContent = res.data.rank
                punch.textContent = res.data.insigniaNum,
                today.textContent = res.data.isPunch === 'true' ?  "已打卡": "今日打卡",
                medal.textContent = res.data.punchIn
            }

        })
    }
    getHomeDate()


    //打卡事件
    today.addEventListener('click', function () {
        //获取本地存储中用户id
        const userId = localStorage.getItem('userId');


        
        //发送请求
        $http.get(`/clockIn?userId=${userId}`, function (res) {


            
            

            console.log(res.status);
            
            if (res.status === 0) {
                today.textContent = '已打卡'  
            }
        })
    })
})